package com.yzx.lifeassistants.model.analysis;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import com.yzx.lifeassistants.bean.LectureInfo;
import com.yzx.lifeassistants.utils.CalendarUtil;
import com.yzx.lifeassistants.utils.LogcatUtils;

/**
 * @Description: 解析查询讲座信息返回的结果
 * @author: yzx
 * @time: 2016-3-8 上午10:26:44
 */
public class QueryLectureInfoAnalysis {

	/**
	 * 
	 * @Description: 查询是否成功
	 */
	public static Boolean isSuccess(String response) {
		Boolean isSuccess = false;
		if (response.contains("Error")) {
			isSuccess = false;
		} else {
			isSuccess = true;
		}
		return isSuccess;
	}

	/**
	 * 
	 * @Description: 解析返回的讲座信息
	 */
	public static LectureInfo getData(String response) {
		LectureInfo lectureInfo = new LectureInfo();
		try {
			JSONObject jsonObject = XML.toJSONObject(response);
			JSONObject object = jsonObject.getJSONObject("activity");
			lectureInfo.setTitle(object.getString("title"));
			lectureInfo.setPublished(object.getString("published"));
			lectureInfo.setTimestamp(CalendarUtil.longToString(
					"yyyy-MM-dd HH:mm:ss", object.getLong("timestamp")));
			lectureInfo.setType(object.getString("type"));
			if (null == object.getString("speaker")
					|| "".equals(object.getString("speaker"))) {

			} else {
				lectureInfo.setSpeaker(object.getString("speaker"));
			}
			lectureInfo.setActivityPlace(object.getString("activityPlace"));
			lectureInfo.setSponsor(object.getString("sponsor"));
			lectureInfo.setActivityStartTime(CalendarUtil.longToString(
					"yyyy/MM/dd HH:mm", object.getLong("activityStartTime")));
			if ("".equals(object.getString("activityEndTime"))) {

			} else {
				lectureInfo.setActivityEndTime(CalendarUtil.longToString(
						"yyyy/MM/dd HH:mm", object.getLong("activityEndTime")));
			}
			lectureInfo
					.setDetail(response.substring(response.indexOf("<detail>"),
							response.indexOf("</detail>")));
		} catch (JSONException e) {
			e.printStackTrace();
			if (null != e.getMessage()) {
				LogcatUtils.e("解析查询讲座返回的结果异常", e.getMessage());
			}
		}
		return lectureInfo;

	}
}
